package tsb.com.html5.controller.common;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tsb.com.html5.common.item.SearchItem;
import tsb.com.html5.common.param.GetSearchResultParam;
import tsb.com.html5.common.service.ICommonService;
import tsb.com.html5.controller.base.BaseController;

@Controller
@RequestMapping("/search")
public class SearchController extends BaseController{
	
	@Resource(name="CommonService")
	private ICommonService commonService;
	
	@RequestMapping(method = RequestMethod.GET)
	protected @ResponseBody Map<String, ? extends Object> search(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap<String, Object> returnMap = new HashMap<String, Object>(); 
		try {

			String searchTerm = request.getParameter("q");
			
			GetSearchResultParam param = new GetSearchResultParam();
			param.setQ(searchTerm);
			
			List<SearchItem> searchResult = commonService.getSearchResult(param);
			
			returnMap.put("ResultList", searchResult);

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return returnMap;
	}
	
}
